package com.ggh.jinjilive.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.cy.dialog.BaseDialog;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.goods.GoodsDetailsMobile;
import com.ggh.jinjilive.mobile.mall.goods.GoodsSpecificationMobile;
import com.ggh.jinjilive.views.tag.FlowTagLayout;
import com.ggh.jinjilive.views.tag.OnTagSelectListener;
import com.ggh.jinjilive.views.tag.adapter.TagAdapter;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;


public class SpecificationsDialog extends BaseDialog {
    //详情
    private GoodsDetailsMobile goodsDetailsMobile;
    //商品图片控件
    private RoundedImageView riv;
    //商品名称
    private TextView tv_name;
    //价格
    private TextView tv_price;
    //规格
    private FlowTagLayout mSizeFlowTagLayout;
    private TagAdapter mSizeTagAdapter;
    //减
    private ImageButton img_subtract;
    //加
    private ImageButton img_add;
    //库存
    private TextView tv_storage;
    //数量
    private int magnitude = 1;
    private EditText et_magnitude;
    //库存数量
    private int repertoryMagnitude = 0;
    //选中的规格
    private GoodsSpecificationMobile mGoodsSpecificationMobile;
    public SpecificationsDialog(Context context,GoodsDetailsMobile goodsDetailsMobile) {
        super(context);
        this.goodsDetailsMobile = goodsDetailsMobile;
    }

    public SpecificationsDialog(Context context, int themeResId) {
        super(context, themeResId);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView(R.layout.dialog_specifications);
        gravity(Gravity.BOTTOM);
        //商品图片控件
        riv = findViewById(R.id.riv);
        Glide.with(riv).load(goodsDetailsMobile.getImg()).into(riv);
        //商品名称
        tv_name = findViewById(R.id.tv_name);
        tv_name.setText(goodsDetailsMobile.getName());
        //价格
        tv_price = findViewById(R.id.tv_price);
        //规格
        mSizeFlowTagLayout = findViewById(R.id.size_flow_layout);
        mSizeFlowTagLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        mSizeTagAdapter = new TagAdapter(getContext());
        mSizeFlowTagLayout.setAdapter(mSizeTagAdapter);
        mSizeFlowTagLayout.setOnTagSelectListener(new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                if (selectedList != null && selectedList.size() > 0) {
                    String currentTab = null;
                    for (int i : selectedList) {
                        currentTab = parent.getAdapter().getItem(i).toString();
                    }
                    List<GoodsSpecificationMobile> goodsSpecificationMobileList = goodsDetailsMobile.getSpecs();
                    for (int i = 0; i < goodsSpecificationMobileList.size();i++){
                        GoodsSpecificationMobile goodsSpecificationMobile = goodsSpecificationMobileList.get(i);
                        if(goodsSpecificationMobile.getName().equals(currentTab)){
                            mGoodsSpecificationMobile = goodsSpecificationMobile;
                            goodsDetailsMobile.getSpecs().get(i).setPitchOn(true);
                            tv_price.setText("¥ " + goodsSpecificationMobileList.get(i).getFee());
                            tv_storage.setVisibility(View.VISIBLE);
                            tv_storage.setText("库存：" + goodsSpecificationMobileList.get(i).getStock());
                            repertoryMagnitude = goodsSpecificationMobileList.get(i).getStock();
                            magnitude = 1;
                            if(null != addItemOnClickListener){
                                addItemOnClickListener.onSkipClicked(0,goodsSpecificationMobile);
                            }
                        }else {
                            goodsDetailsMobile.getSpecs().get(i).setPitchOn(false);
                        }
                    }
                }else{
                    mGoodsSpecificationMobile = null;
                    for (int i = 0; i < goodsDetailsMobile.getSpecs().size();i++){
                        goodsDetailsMobile.getSpecs().get(i).setPitchOn(false);
                    }
                    repertoryMagnitude = 0;
                    tv_price.setText("¥ " + 0);
                    tv_storage.setVisibility(View.GONE);
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(1,null);
                    }
                }
            }
        });
        List<String> dataSource = new ArrayList<>();
        for (GoodsSpecificationMobile goodsSpecificationMobile : this.goodsDetailsMobile.getSpecs()){
            dataSource.add(goodsSpecificationMobile.getName());
        }
        mSizeTagAdapter.clearAndAddAll(dataSource);


        //库存
        tv_storage = findViewById(R.id.tv_storage);

        //减
        img_subtract = findViewById(R.id.img_subtract);
        img_subtract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(magnitude == 1){
                    ToastUtils.make().show("数量低于范围~");
                    return;
                }
                magnitude --;
                et_magnitude.setText(magnitude +"");
            }
        });
        //加
        img_add = findViewById(R.id.img_add);
        img_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(magnitude < repertoryMagnitude){
                    magnitude ++;
                    et_magnitude.setText(magnitude +"");
                }
            }
        });
        //数量
        et_magnitude = findViewById(R.id.et_magnitude);
        et_magnitude.setText(magnitude +"");
        //加入购物车
        findViewById(R.id.Button_join_shopping).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(null != mGoodsSpecificationMobile){
                    if(null != addItemOnClickListener){
                        //购买数量
                        mGoodsSpecificationMobile.setGoodsQuantity(magnitude);
                        addItemOnClickListener.onSkipClicked(2,mGoodsSpecificationMobile);
                    }
                }else {
                    ToastUtils.make().show("请先选择规则");
                }
            }
        });
        //立即下单
        findViewById(R.id.Button_Place).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(null != mGoodsSpecificationMobile){
                    if(null != addItemOnClickListener){
                        //购买数量
                        mGoodsSpecificationMobile.setGoodsQuantity(magnitude);
                        addItemOnClickListener.onSkipClicked(3,mGoodsSpecificationMobile);
                    }
                }else {
                    ToastUtils.make().show("请先选择规则");
                }
            }
        });
    }




    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener){
        this.addItemOnClickListener = addItemOnClickListener;
    }


    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }
}
